package io.renren.wechat.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/redis")
public class RedisTestController {
    private static final Logger LOGGER = LoggerFactory.getLogger(RedisTestController.class);
    private StringRedisTemplate stringRedisTemplate;


    @RequestMapping(value = "/set", method = RequestMethod.GET)
    public String setValue(@RequestParam(value = "key", required = false, defaultValue = "name") String key,
                           @RequestParam(value = "value") String value) throws Exception {
        //return redisService.setStringValue(key, value);
        stringRedisTemplate.opsForValue().set(key, value);
        LOGGER.debug("setting redis cache for key: [{}], value: [{}]", key, value);
        return stringRedisTemplate.opsForValue().get(key);
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public String getValue(@RequestParam(value = "key", required = false, defaultValue = "name") String key) {
        //return redisService.getStringValue(key);
        final String value = stringRedisTemplate.opsForValue().get(key);

        LOGGER.debug("got redis cache for key: [{}], and value: [{}]", key, value);

        return value;
    }


    @Autowired
    public void setStringRedisTemplate(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }
}
